package biolean.files;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Obsahuje funkcie pre citanie a zapis anotacneho suboru po riadkoch.
 * @author Rasto
 */
public class AnnotationFile extends BioFile 
{
	public AnnotationFile()
	{
		super(0,0, "");
	}
	
	public AnnotationFile(int x, int y, String fileName)
	{
		super( x, y, fileName);
	}
	
	/**
	 * Precita anotaciu zo suboru.
	 * @return Anotacia.
	 */
	public Annotation readAnn() throws IOException
	{
		String line;
		while ((line = mReader.readLine() ) != null) 
		{
			if (isAnnotationLine(line)) 
			{
				return new Annotation(line);
			}
		}
		return null;
	}
	
	/**
	 * Zapise cele pole anotacii do anotacneho suboru.
	 * @param - Pole riadkov.
	 */
	public void writeFile(List<Annotation> anns) throws IOException
	{
		for (Annotation ann: anns) 
		{
			mWriter.write(ann.getTheLine());
			mWriter.write("\n");
		}
	}
	
	/**
	 * Precita cely anotacny subor do listu anotacii
	 */
	public List<Annotation> readFile() throws IOException
	{
		List<Annotation> anns = new ArrayList<Annotation>();
		Annotation ann;
		while ((ann = readAnn()) != null) 
		{
			anns.add(ann);
		}
		return anns;
	}

	/**
	 * Kontrolna funkcia.
	 * @param line - Riadok anotacneho suboru.
	 * @return Vracia true, ak riadok nie je prazdny.
	 */
	private boolean isAnnotationLine(String line)
	{
		return line.length() > 0;
	}
}
